package pathFinder.bfs;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;


public class Path {
		
	private ArrayList<Edge> connection;
	public float distance;
	public float price;
	public ArrayList<Integer> fightIdList;
	private String PADDING = "------------------";
	public Path(ArrayList<Edge> connection){
		this.connection = connection;
		distance = computeDistance();
		fightIdList = getFlightIdList();
		price = computePrice();
	}
	
	public int getNrConnections(){
		return connection.size();
	}
	//(pentru fiecare zbor, ID-ul, sursa, destinatia, ora de plecare, durata etc.)
	private String dateFormating(int date){
		// Date date; // your date
			Date d =  new Date(date);
		    Calendar cal = Calendar.getInstance();
		    cal.setTime(d);
		    int year = cal.get(Calendar.YEAR);
		    int month = cal.get(Calendar.MONTH);
		    int day = cal.get(Calendar.DAY_OF_MONTH);
		    return day+"//"+month+"//"+year;
		
	}
	public String toString(){
		String rez=PADDING+"\n";
	
		int departHour = connection.get(0).getHourDeparture();
		int previousArrivalHour = departHour;
		for(int i = 0; i < connection.size(); i++){
			int delay = connection.get(i).getHourDeparture() - previousArrivalHour;
			if(delay>0){
				rez+= "wait( "+delay+"hours )...\n";
			}
			rez+= (i+1)+". flight:"+connection.get(i).getIdFlight()+
					" from "+connection.get(i).from+
					" to "+ connection.get(i).to+
					" on "+ dateFormating(connection.get(i).getDepartureDay())+
					" at "+connection.get(i).getHourDeparture()%24+":00 "+
					"for "+connection.get(i).getDurationFlight()+"hour"+
							(connection.get(i).getDurationFlight()>1?"s":"")+" \n"
					+"\t\t\t\t(price:"+connection.get(i).getPrice()+")\n";
			
			previousArrivalHour = connection.get(i).getHourDeparture() +  connection.get(i).getDurationFlight();
		}
		rez+=PADDING;
		return rez;
		
	}
	
	private float computeDistance(){
		
		int legaturi = connection.size();
		
		if(legaturi == 0)
			return 0;
		
		float departureHour = connection.get(0).getHourDeparture();
		float arrivalHour = connection.get(legaturi-1).getHourDeparture()+connection.get(legaturi-1).getDurationFlight();
		return arrivalHour - departureHour;
		
	}
	
	private float computePrice(){
		float price = 0;
		for(int i = 0; i < connection.size(); i++){
			price +=connection.get(i).getPrice();
		}
		return price;
		
	}
	private ArrayList<Integer> getFlightIdList(){
		
		ArrayList<Integer> flights = new ArrayList<>();
		for(int i = 0; i < connection.size(); i++){
			flights.add(connection.get(i).getIdFlight());
		}
		return flights;
	}
	
	public float getDepartureHour(){
		
		if(connection.size() > 0)
			return connection.get(0).getHourDeparture();
		return  -1;
	}
	
	public float getArrivalHour(){
		
		if(connection.size() > 0)
			return connection.get(connection.size()-1).getHourDeparture() + connection.get(connection.size()-1).getDurationFlight() ;
		return  -1;
	}
	
	public float getPrice(){
		
		return price;
	}
}
